// pages/manufacturer/manufacturer.js
import { Factory } from "../../models/factory";
Page({
  /**
   * 页面的初始数据
   */
  data: {
    tabList: [],
    activeTab: 0,
    proList: [],
    currentItem: "全部",
    showMore1: true,
    showMore2: true,
    currentList: [],
    currentList1: [],
    currentList2: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.init2();
    this.init();
  },

  async init2() {
    let postData = {
      Search: { all: true, userId: this.userId, keyword: null },
      PageIndex: 1,
      PageRows: 10000,
      SortField: "Order",
      SortType: "asc"
    };
    const data2 = await Factory.GetDataListClassify(postData);
    data2.Data.map(item => {
      item.txt = item.Name;
    });
    this.setData({
      tabList: data2.Data
    });
  },
  onClickTab(name, title) {
    this.showMore1 = true;
    this.showMore2 = true;
    this.activeTab = name;
    this.currentItem = title;
  },
  changeTab(index, item) {
    this.showMore1 = true;
    this.showMore2 = true;
    this.activeTab = index;
    this.currentItem = item.txt;
  },
  toDetail(e) {
    let dataset = e.currentTarget.dataset;
    console.log(e.currentTarget.dataset);
    // this.$router.push({
    //   path: "/manufacturer-detail",
    //   query: {
    //     id: item.Id,
    //     User_id: item.User_id
    //   }
    // });
    wx.navigateTo({
      url: `../manufacturer-detail/manufacturer-detail?id=${dataset.id}&user_id=${dataset.userid}`
    });
  },
  async init() {
    let postData = {
      Search: { all: true, userId: "", keyword: null },
      PageIndex: 1,
      PageRows: 10000,
      SortField: "id",
      SortType: "asc"
    };
    const data = await Factory.GetDataList(postData);
    data.Data.map(item => {
      item.img = item.Imags.split(",")[0];
      return item;
    });

    this.data.currentList = JSON.parse(JSON.stringify(data.Data));
    this.data.proList = JSON.parse(JSON.stringify(data.Data));
    let currentList1 = this.currentList1();
    let currentList2 = this.currentList2();
    this.setData({
      currentList1,
      currentList2
    });
  },
  // 已认证
  currentList1() {
    let data = this.data.currentList.filter(item => {
      return item.Status == 1;
    });
    if (this.data.showMore1) {
      return data;
    } else {
      return data.slice(0, 2);
    }
  },
  // 未认证
  currentList2() {
    let data = this.data.currentList.filter(item => {
      return item.Status == 0;
    });
    if (this.data.showMore2) {
      return data;
    } else {
      return data.slice(0, 2);
    }
  },
  handleShowMore1() {
    let data = !this.data.showMore1;
    this.data.showMore1 = data;
    this.setData({
      showMore1: data,
      currentList1: this.currentList1()
    });
  },
  handleShowMore2() {
    let data = !this.data.showMore2;
    this.data.showMore2 = data;
    this.setData({
      showMore2: data,
      currentList2: this.currentList2()
    });
  },
  tabChange(e) {
    // if (this.currentItem === "全部") {
    //   return this.proList
    // } else {
    //   return this.proList.filter(item => {
    //     return this.currentItem === item.ProductClsName
    //   })
    // }
    this.data.showMore1 = true;
    this.data.showMore2 = true;
    this.data.currentItem = e.detail.title;
    let currentList = [];
    if (e.detail.index == 0) {
      currentList = JSON.parse(JSON.stringify(this.data.proList));
    } else {
      let list = this.data.proList.filter(item => {
        return this.data.currentItem === item.ProductClsName;
      });
      currentList = JSON.parse(JSON.stringify(list));
    }
    this.data.currentList = currentList;
    this.setData({
      showMore1: true,
      showMore2: true,
      currentList1: this.currentList1(),
      currentList2: this.currentList2()
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {}
});
